/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.tools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Austin
 */
public class TimeFormat {

    //时间转换
    public long getTimestamp(String sTime) {
        long rand = -99;
        try {
            Timestamp ts = Timestamp.valueOf(sTime);
            Date date2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("1970/01/01 08:00:00");
            long l = ts.getTime() - date2.getTime() > 0 ? ts.getTime() - date2.getTime() : date2.getTime() - ts.getTime();
            rand = (int) (l / 1000);

        } catch (ParseException ex) {
            Logger.getLogger(TimeFormat.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return rand;
        }
    }

    public String getTimeString(Long i) {
        String time = String.valueOf(i) + "000";
        Long j = Long.valueOf(time);
        Date d = new Date(j);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH");
        return sdf.format(d);
    }

    //获取当前系统时间
    public long getNewTime() {
        Long nowTime = System.currentTimeMillis() / 1000;
        return nowTime;
    }

    public static void main(String[] args) throws ParseException, UnsupportedEncodingException {
//        Long s = 1492393499L;
//        String time = String.valueOf(s);
//        Long j = Long.valueOf(time);
//        Date d = new Date(j);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH");
//        System.out.println(sdf.parse(s + ""));
//        System.out.println(new TimeFormat().getTimeString(s));
//        Long ct = Long.valueOf("1492393499");
//        SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日 HH时");
//        String ct2day = sf.format(new Date(ct * 1000));
//        System.out.println(ct2day);
//        System.out.println("1498300961".substring(0,"1498300961".length()-2));
        System.out.println(getTime(System.currentTimeMillis() / 1000));
    }

    public static Long getTime(Long ct) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm");
        String ct2day = sf.format(new Date(ct * 1000));
        if (Long.valueOf(ct2day.substring(ct2day.length() - 1, ct2day.length())) >= 5) {
            ct2day = ct2day.substring(0, ct2day.length() - 1) + "5";
        } else {
            ct2day = ct2day.substring(0, ct2day.length() - 1) + "0";
        }
        String user_time = ct2day + "分00秒";
        String re_time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        Date d;
        try {
            d = sdf.parse(user_time);
            long l = d.getTime();
            String str = String.valueOf(l);
            re_time = str.substring(0, 10);
        } catch (ParseException e) {
// TODO Auto-generated catch block  
            e.printStackTrace();
        }
        return Long.valueOf(re_time);
    }
}
